package com.lipaluma.field.mapper.builder;

import java.lang.reflect.Field;
import java.util.Locale;

import com.lipaluma.context.Context;
import com.lipaluma.context.mapping.impl.BuilderMappingManager;
import com.lipaluma.field.mapper.FieldMapper;
import com.lipaluma.field.mapper.initializer.FieldMapperInitializer;
import com.lipaluma.field.mapper.mapping.FieldMappingInfo;

public class InitMappingBuilder {
	private FieldMapper fieldMapper;
	private Field field;
	private FieldMapperBuilder parent;
	private Context context;
	
	public InitMappingBuilder(FieldMapperBuilder parent, FieldMapper fieldMapper, Field field) {
		this.parent = parent;
		this.context = parent.getContext();
		this.fieldMapper = fieldMapper;
		this.field = field;
	}
	public InitMappingBuilder(Context context, FieldMapper fieldMapper, Field field) {
		this.context = context;
		this.fieldMapper = fieldMapper;
		this.field = field;
	}
	
	public InitMappingBuilder on(FieldMappingInfo... mappings) {
		context.getMappingManagerAs(BuilderMappingManager.class).addMappings(field, mappings);
		return this;
	}
	public InitMappingBuilder withPattern(String pattern) {
		context.getMappingManagerAs(BuilderMappingManager.class).addPattern(field, pattern);
		return this;
	}
	public InitMappingBuilder withSeparator(String separator) {
		context.getMappingManagerAs(BuilderMappingManager.class).addSeparator(field, separator);
		return this;
	}
	public InitMappingBuilder withLocale(Locale locale) {
		context.getMappingManagerAs(BuilderMappingManager.class).addLocale(field, locale);
		return this;
	}
	public InitMappingBuilder withTrueValues(String... trueValues) {
		context.getMappingManagerAs(BuilderMappingManager.class).addTrueValues(field, trueValues);
		return this;
	}
	public InitMappingBuilder withFalseValues(String... falseValues) {
		context.getMappingManagerAs(BuilderMappingManager.class).addFalseValues(field, falseValues);
		return this;
	}
	public InitMappingBuilder keepAll() {
		context.getMappingManagerAs(BuilderMappingManager.class).isKeepAll(field, true);
		return this;
	}
	public FieldMapperAfterRegisterBuilder endMap() {
		if(FieldMapperInitializer.class.isAssignableFrom(fieldMapper.getClass()))
			((FieldMapperInitializer)fieldMapper).init(context, field);
		return parent;
	}
}
